package cn.tedu.qxhouserent.house.service.impl;

import cn.tedu.qxhouserent.commons.ex.ServiceException;
import cn.tedu.qxhouserent.commons.pojo.vo.PageData;
import cn.tedu.qxhouserent.commons.web.ServiceCode;
import cn.tedu.qxhouserent.house.mapper.*;

import cn.tedu.qxhouserent.house.pojo.entity.*;

import cn.tedu.qxhouserent.house.pojo.param.*;

import cn.tedu.qxhouserent.house.pojo.vo.*;
import cn.tedu.qxhouserent.house.service.HouseService;
import cn.tedu.qxhouserent.commons.util.PageInfoToPageDataConverter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Slf4j
@Service
public class HouseServiceImpl implements HouseService {

    @Autowired
    private HouseMapper houseMapper;
    @Autowired
    private LeaseHouseMapper leaseHouseMapper;
    @Autowired
    private NewHouseMapper newHouseMapper;
    @Autowired
    private SecondHandHouseMapper secondHandHouseMapper;

    @Override
    public void addLeaseHouse(LeaseHouseAddNewParam leaseHouseAddNewParam) {
        log.debug("开始处理【添加出租房源】的业务，参数：{}", leaseHouseAddNewParam);

        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", leaseHouseAddNewParam.getName());
        int countByName = houseMapper.selectCount(queryWrapper);

        log.debug("根据房源标题统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "添加房源失败,房源标题已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);

        }

        //写入house表中
        House house = new House();
        BeanUtils.copyProperties(leaseHouseAddNewParam, house);
        house.setGmtCreated(LocalDateTime.now());
        house.setGmtModified(LocalDateTime.now());

        int rows = houseMapper.insert(house);
        if (rows != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        //写入lease_house表中
        LeaseHouse leaseHouse = new LeaseHouse();
        BeanUtils.copyProperties(leaseHouseAddNewParam, leaseHouse);
        leaseHouse.setHouseId(house.getId());
        int insert = leaseHouseMapper.insert(leaseHouse);
        if (insert != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的房源数据写入到数据库，完成！");
    }

    @Override
    public void addNewHouse(NewHouseAddNewParam newHouseAddNewParam) {
        log.debug("开始处理【添加新房源】的业务，参数：{}", newHouseAddNewParam);
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", newHouseAddNewParam.getName());
        int countByName = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源标题统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "添加房源失败,房源标题已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);

        }

        //写入house表中
        House house = new House();
        BeanUtils.copyProperties(newHouseAddNewParam, house);
        house.setGmtCreated(LocalDateTime.now());
        house.setGmtModified(LocalDateTime.now());

        int rows = houseMapper.insert(house);
        if (rows != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        //写入new_house表中
        NewHouse newHouse = new NewHouse();
        BeanUtils.copyProperties(newHouseAddNewParam, newHouse);
        newHouse.setHouseId(house.getId());
        int insert = newHouseMapper.insert(newHouse);
        if (insert != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的房源数据写入到数据库，完成！");
    }

    @Override
    public void addSecondHandHouse(SecondHandHouseAddNewParam secondHandHouseAddNewParam) {
        log.debug("开始处理【添加二手房源】的业务，参数：{}", secondHandHouseAddNewParam);
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", secondHandHouseAddNewParam.getName());
        int countByName = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源标题统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "添加房源失败,房源标题已被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);

        }

        //写入house表中
        House house = new House();
        BeanUtils.copyProperties(secondHandHouseAddNewParam, house);
        house.setGmtCreated(LocalDateTime.now());
        house.setGmtModified(LocalDateTime.now());

        int rows = houseMapper.insert(house);
        if (rows != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        //写入second_hand_house表中
        SecondHandHouse secondHandHouse = new SecondHandHouse();
        BeanUtils.copyProperties(secondHandHouseAddNewParam, secondHandHouse);
        secondHandHouse.setHouseId(house.getId());
        int insert = secondHandHouseMapper.insert(secondHandHouse);
        if (insert != 1) {
            String message = "添加房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的房源数据写入到数据库，完成！");
    }

    @Override
    public void deleteLeaseHouseById(Long id) {
        log.debug("开始处理【通过id删除房源】的业务，参数：{}", id);
        //检查房源id是否存在
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countByName = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源id统计匹配的房源数量，结果：{}", countByName);
        if (countByName == 0) {
            String message = "删除房源失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 出租房源表关联到了此房源，删除
        leaseHouseMapper.deleteByHouseId(id);

        //执行删除
        int rows = houseMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        log.debug("通过id删除房源数据，完成！");
    }

    @Override
    public void deleteNewHouseById(Long id) {
        log.debug("开始处理【通过id删除房源】的业务，参数：{}", id);
        //检查房源id是否存在
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countByName = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源id统计匹配的房源数量，结果：{}", countByName);
        if (countByName == 0) {
            String message = "删除房源失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 新房源表关联到了此房源，删除
        newHouseMapper.deleteByHouseId(id);

        //执行删除
        int rows = houseMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        log.debug("通过id删除房源数据，完成！");
    }

    @Override
    public void deleteSecondHandHouseById(Long id) {
        log.debug("开始处理【通过id删除房源】的业务，参数：{}", id);
        //检查房源id是否存在
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countByName = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源id统计匹配的房源数量，结果：{}", countByName);
        if (countByName == 0) {
            String message = "删除房源失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 二手房源表关联到了此房源，删除
        int i = secondHandHouseMapper.deleteByHouseId(id);
        if (i != 1) {
            String message = "删除房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        //执行删除
        int rows = houseMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除房源失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        log.debug("通过id删除房源数据，完成！");
    }

    @Override
    public void updateLeaseHouseById(Long id, LeaseHouseUpdateInfoParam leaseHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源详情】的业务，ID：{}, 新数据：{}", id, leaseHouseUpdateInfoParam);
        // 检查房源是否存在，如果不存在，则抛出异常
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源ID统计匹配的房源数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查房源名称是否被其它房源占用，如果被占用，则抛出异常
        QueryWrapper<House> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name", leaseHouseUpdateInfoParam.getName())
                .ne("id", id);
        int countByName = houseMapper.selectCount(queryWrapper2);
        log.debug("根据房源名称统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "修改房源详情失败，房源名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 执行修改
        House house = new House();
        BeanUtils.copyProperties(leaseHouseUpdateInfoParam, house);
        house.setId(id);

        int rows = houseMapper.updateById(house);
        if (rows != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        LeaseHouse leaseHouse = new LeaseHouse();
        leaseHouse.setLeaseTime(leaseHouseUpdateInfoParam.getLeaseTime());
        QueryWrapper<LeaseHouse> leaseHouseQueryWrapper = new QueryWrapper<>();
        leaseHouseQueryWrapper.eq("house_id", house.getId());
        int update = leaseHouseMapper.update(leaseHouse, leaseHouseQueryWrapper);
        if (update != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        log.debug("将新的房源数据更新入到数据库，完成！");
    }

    @Override
    public void updateNewHouseById(Long id, NewHouseUpdateInfoParam newHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源详情】的业务，ID：{}, 新数据：{}", id, newHouseUpdateInfoParam);
        // 检查房源是否存在，如果不存在，则抛出异常
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源ID统计匹配的房源数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查房源名称是否被其它房源占用，如果被占用，则抛出异常
        QueryWrapper<House> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name", newHouseUpdateInfoParam.getName())
                .ne("id", id);
        int countByName = houseMapper.selectCount(queryWrapper2);
        log.debug("根据房源名称统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "修改房源详情失败，房源名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 执行修改
        House house = new House();
        BeanUtils.copyProperties(newHouseUpdateInfoParam, house);
        house.setId(id);

        int rows = houseMapper.updateById(house);
        if (rows != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        NewHouse newHouse = new NewHouse();
        newHouse.setOpeningTime(newHouseUpdateInfoParam.getOpeningTime())
                .setTermOfOwnership(newHouseUpdateInfoParam.getTermOfOwnership())
                .setTimeOfDelivery(newHouseUpdateInfoParam.getTimeOfDelivery());
        QueryWrapper<NewHouse> newHouseQueryWrapper = new QueryWrapper<>();
        newHouseQueryWrapper.eq("house_id", house.getId());
        int update = newHouseMapper.update(newHouse, newHouseQueryWrapper);
        if (update != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        log.debug("将新的房源数据更新入到数据库，完成！");
    }

    @Override
    public void updateSecondHandHouseById(Long id, SecondHandHouseUpdateInfoParam secondHandHouseUpdateInfoParam) {
        log.debug("开始处理【修改房源详情】的业务，ID：{}, 新数据：{}", id, secondHandHouseUpdateInfoParam);
        // 检查房源是否存在，如果不存在，则抛出异常
        QueryWrapper<House> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        int countById = houseMapper.selectCount(queryWrapper);
        log.debug("根据房源ID统计匹配的房源数量，结果：{}", countById);
        if (countById == 0) {
            String message = "修改房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查房源名称是否被其它房源占用，如果被占用，则抛出异常
        QueryWrapper<House> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("name", secondHandHouseUpdateInfoParam.getName())
                .ne("id", id);
        int countByName = houseMapper.selectCount(queryWrapper2);
        log.debug("根据房源名称统计匹配的房源数量，结果：{}", countByName);
        if (countByName > 0) {
            String message = "修改房源详情失败，房源名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 执行修改
        House house = new House();
        BeanUtils.copyProperties(secondHandHouseUpdateInfoParam, house);
        house.setId(id);

        int rows = houseMapper.updateById(house);
        if (rows != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        SecondHandHouse secondHandHouse = new SecondHandHouse();
        secondHandHouse.setHouseAge(secondHandHouseUpdateInfoParam.getHouseAge());
        QueryWrapper<SecondHandHouse> secondHandHouseQueryWrapper = new QueryWrapper<>();
        secondHandHouseQueryWrapper.eq("house_id", house.getId());
        int update = secondHandHouseMapper.update(secondHandHouse, secondHandHouseQueryWrapper);
        if (update != 1) {
            String message = "修改失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        log.debug("将新的房源数据更新入到数据库，完成！");
    }

    @Override
    public void setRecom(Long id) {
        updateRecomById(id, 1);
    }

    @Override
    public void setNotRecom(Long id) {
        updateRecomById(id, 0);
    }

    private void updateRecomById(Long id, Integer recom) {
        log.debug("开始处理【{}房源】的业务，ID：{}，目标状态：{}", RECOM_TEXT[recom], id, recom);
        // 检查数据是否存在
        HouseStandardVO houseStandardVO = houseMapper.getStandardById(id);
        if (houseStandardVO == null) {
            String message = RECOM_TEXT[recom] + "房源失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        // 检查当前状态是否与参数表示的状态相同
        if (houseStandardVO.getRecom().equals(recom)) {
            String message = RECOM_TEXT[recom] + "房源失败，当前房源已经处于"
                    + RECOM_TEXT[recom] + "状态！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 准备执行更新
        House house = new House();
        house.setId(id);
        house.setRecom(recom);
        log.debug("即将修改数据，参数：{}", house);
        int rows = houseMapper.updateById(house);
        if (rows != 1) {
            String message = RECOM_TEXT[recom] + "房源失败，服务器忙，请稍后再次尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public PageData<HouseListItemVO> list(Integer pageNum) {
        Integer pageSize = 5;
        return list(pageNum, pageSize);
    }

    @Override
    public PageData<HouseListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询房源列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<HouseListItemVO> list = houseMapper.list();
        PageInfo<HouseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<HouseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    @Override
    public HouseStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询房源详情】的业务，参数：{}", id);
        HouseStandardVO queryResult = houseMapper.getStandardById(id);
        if (queryResult == null) {
            String message = "查询房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<LeaseHouseListItemVO> listLeaseHouse(Integer pageNum) {
        Integer pageSize = 9;
        return listLeaseHouse(pageNum, pageSize);
    }

    @Override
    public PageData<LeaseHouseListItemVO> listLeaseHouse(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询房源列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<LeaseHouseListItemVO> list = houseMapper.listLeaseHouse();
        PageInfo<LeaseHouseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<LeaseHouseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    @Override
    public LeaseHouseStandardVO getStandardLeaseHouse(Long id) {
        log.debug("开始处理【根据ID查询房源详情】的业务，参数：{}", id);
        LeaseHouseStandardVO queryResult = houseMapper.getStandardLeaseHouse(id);
        if (queryResult == null) {
            String message = "查询房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<NewHouseListItemVO> listNewHouse(Integer pageNum) {
        Integer pageSize = 9;
        return listNewHouse(pageNum, pageSize);
    }

    @Override
    public PageData<NewHouseListItemVO> listNewHouse(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询房源列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<NewHouseListItemVO> list = houseMapper.listNewHouse();
        PageInfo<NewHouseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<NewHouseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    @Override
    public NewHouseStandardVO getStandardNewHouse(Long id) {
        log.debug("开始处理【根据ID查询房源详情】的业务，参数：{}", id);
        NewHouseStandardVO queryResult = houseMapper.getStandardNewHouse(id);
        if (queryResult == null) {
            String message = "查询房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<SecondHandHouseListItemVO> listSecondHandHouse(Integer pageNum) {
        Integer pageSize = 9;
        return listSecondHandHouse(pageNum, pageSize);
    }

    @Override
    public PageData<SecondHandHouseListItemVO> listSecondHandHouse(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询房源列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<SecondHandHouseListItemVO> list = houseMapper.listSecondHandHouse();
        PageInfo<SecondHandHouseListItemVO> pageInfo = new PageInfo<>(list);
        PageData<SecondHandHouseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        log.debug("查询完成，即将返回：{}", pageData);
        return pageData;
    }

    @Override
    public SecondHandHouseStandardVO getStandardSecondHandHouse(Long id) {
        log.debug("开始处理【根据ID查询房源详情】的业务，参数：{}", id);
        SecondHandHouseStandardVO queryResult = houseMapper.getStandardSecondHandHouse(id);
        if (queryResult == null) {
            String message = "查询房源详情失败，房源数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public List<RecomHouseListItemVO> listRecom() {
        List<RecomHouseListItemVO> houses = houseMapper.listRecom();
        return houses;
    }
    //二手推荐房列表
    @Override
    public List<RecomHouseListItemVO> ershoulistRecom() {
        List<RecomHouseListItemVO> houses = houseMapper.ershoulistRecom();
        return houses;
    }
    //买推荐房列表
    @Override
    public List<RecomHouseListItemVO> mailistRecom() {
        List<RecomHouseListItemVO> houses = houseMapper.mailistRecom();
        return houses;
    }

    @Override
    public List<HouseListItemVO> cha(String name, Long hcid) {
        Long hid;
        if (hcid != null){
            hid = hcid;
        }else hid = -1L;

        List<HouseListItemVO> l1 = houseMapper.cha(name, hid);
        List<HouseListItemVO> l2 = houseMapper.cha2(name, hid);
        List<HouseListItemVO> l = Stream.concat(l1.stream(), l2.stream()).collect(Collectors.toList());
        Integer[] s = houseMapper.sectionSelect(name);
        for (Integer s1:s){
            List<HouseListItemVO> l3 = houseMapper.cha3(s1, hid);
            l.addAll(l3);
        }
        return l;
    }

    @Override
    public PageData<HouseListItemVO> chaxiang(HouseSelectParam houseSelectParam,Integer page) {
        String price = houseSelectParam.getPrice();
        String area = houseSelectParam.getArea();
        String floor = houseSelectParam.getFloor();
        String has = houseSelectParam.getHasElevator();
        String recom = houseSelectParam.getRecom();
        String sectionId = houseSelectParam.getSectionId();
        String time = houseSelectParam.getLeaseTime();
        String type = houseSelectParam.getLeaseType();
        String age = houseSelectParam.getHouseAge();
        Long hcid = houseSelectParam.getHouseCategoryId();
        Integer h;
        Integer r;
        Integer s;
        Integer t;
        Long hid;
        if (hcid != null) {
            hid = hcid;
        } else hid = -1L;
        if (has != null && has != "") {
            h = Integer.parseInt(has);
        } else h = -1;
        if (recom != null && recom != ""){
        r= Integer.parseInt(recom);
        }else r = -1;
        if (sectionId != null && sectionId != ""){
            s = Integer.parseInt(sectionId);
        }else s = -1;
        if (type != null && type != ""){
           t = Integer.parseInt(type);
        }else t = -1;
        Integer[] p = new Integer[2];
        Integer[] areaArr = new Integer[2];
        Integer[] f = new Integer[2];
        Integer[] timeArr = new Integer[2];
        Integer[] ageArr = new Integer[2];
        p = extracted(price, p);
        areaArr = extracted(area, areaArr);
        f = extracted(floor, f);
        timeArr = extracted(time, timeArr);
        ageArr = extracted(age, ageArr);
        Integer p0 = p[0];
        Integer p1;
        if (p.length == 1){
            p1 = -1;
        }else p1 = p[1];
        Integer areaArr0 = areaArr[0];
        Integer areaArr1;
        if (areaArr.length == 1){
            areaArr1 = -1;
        }else areaArr1 = areaArr[1];
        Integer f0 = f[0];
        Integer f1;
        if (f.length == 1){
            f1 = -1;
        }else f1 = f[1];
        Integer timeArr0 = timeArr[0];
        Integer timeArr1;
        if (timeArr.length == 1){
            timeArr1 = -1;
        }else timeArr1 = timeArr[1];
        Integer ageArr0 = ageArr[0];
        Integer ageArr1;
        if (ageArr.length == 1){
            ageArr1 = -1;
        }else ageArr1 = ageArr[1];

        PageHelper.startPage(page, 9);
        List<HouseListItemVO> l = houseMapper.chaxiang(p0, p1, areaArr0, areaArr1, f0, f1, timeArr0, timeArr1, ageArr0, ageArr1, h, r, s, t, hid);
        PageInfo<HouseListItemVO> pageInfo = new PageInfo<>(l);
        PageData<HouseListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);

        log.debug("返回参数：{}", pageData);

        return pageData;
    }

    private Integer[] extracted(String price, Integer[] p) {
        if (price != "" && price != null) {
            String[] pr = price.split("-");
            if (pr.length == 1){
                pr[0]=pr[0].substring(0, pr[0].length() - 2);
            }
            p = Arrays.stream(pr).map(Integer::parseInt).toArray(Integer[]::new);
            log.debug("信息：----------------{}", p);
        }
        return p;
    }
}
